package controllers.portal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Menu;
import rbac.models.Oplog;
import models.Infotype;
import rbac.models.User;

import com.et.mvc.JsonView;
import com.et.mvc.View;

import controllers.ApplicationController;

public class InfotypeController extends ApplicationController {
	public void index(){
		
	}
	
	public View getAll() throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		List<Infotype> items = Infotype.findAll(Infotype.class, null, null, "sort");
		result.put("total", items.size());
		result.put("rows", items);
		return new JsonView(result);
	}
	
	public View getTypes() throws Exception {
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		Map<String,Object> node = new HashMap<String,Object>();
		node.put("id", 0);
		node.put("text", "全部类型");
		result.add(node);
		List<Map<String,Object>> children = new ArrayList<Map<String,Object>>();
		List<Infotype> items = Infotype.findAll(Infotype.class, null, null, "sort");
		for(Infotype item: items){
			Map<String,Object> child = new HashMap<String,Object>();
			child.put("id", item.getId());
			child.put("text", item.getName());
			children.add(child);
		}
		node.put("children", children.toArray(new Object[children.size()]));
		return new JsonView(result);
	}
	
	public View save(Infotype type) throws Exception {
		Integer sort = (Integer)Infotype.maximum(Infotype.class, "sort", null, null);
		if (sort == null){
			sort = 1;
		} else {
			sort += 1;
		}
		type.setSort(sort);
		type.save();
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建资讯类型，内容为：" + new JsonView(type));									

		Map<String,Object> result = new HashMap<String,Object>();
		result.put("success", true);
		result.put("row", type);
		return new JsonView(result);
	}
	
	public View update(int id, Map<String, Object> params) throws Exception {
		Infotype type = Infotype.find(Infotype.class, id);
		JsonView olditem= new JsonView(type);
		type = Infotype.updateModel(type, params);
		type.save();
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改资讯类型，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(type));										

		Map<String,Object> result = new HashMap<String,Object>();
		result.put("success", true);
		result.put("row", type);
		return new JsonView(result);
	}
	
	public View destroy(int id) throws Exception {
		Infotype type = Infotype.find(Infotype.class, id);
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除资讯类型，内容为：" + new JsonView(type));									

		type.destroy();
		return new JsonView("success:true");
	}
	
	public View move(int id, String dir) throws Exception {
		Infotype item = Infotype.find(Infotype.class, id);
		Infotype target = null;
		if (dir.equals("down")) {
			target = Infotype.findFirst(Infotype.class, "sort>?",
					new Object[] { item.getSort() },
					"sort asc");
		} else {
			target = Infotype.findFirst(Infotype.class, "sort<? ",
					new Object[] { item.getSort() },
					"sort desc");
		}
		if (target != null) {
			Integer tmp = target.getSort();
			target.setSort(item.getSort());
			target.save();
			item.setSort(tmp);
			item.save();
		}
		return new JsonView("success:true");
	}
	
}
